package kr.or.smardi.smardi_AScan.Record.graph;

import java.util.*;
import android.content.*;
import android.graphics.*;
import android.util.*;
import android.view.*;
import android.view.View.*;

public class MyView extends View {
	int width;
	int height;
	float minX, maxX, minY, maxY;
	
	public static final int UNIT_DAY = 1;
	public static final int UNIT_10MIN = 2;
	
	Graph graph;
	ArrayList<GraphData> graphData;
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		graph.setSize(width, height);
		//graph.show(canvas, Pnt);
		graph.makeGraph(canvas, true, true);
	}
	
	
	//첫번째로 graphData를 만들고
	//두번째로 값을 넣고
	//세번째로 값을 저장함
	public void makeGraphData() {
		graphData = new ArrayList<GraphData>();
	}
	
	public void addGraphData(long x, double y) {
		graphData.add(new GraphData(x, y));
	}
	
	public void saveGraphData(int color, String title) {
		graph.addDataArray(graphData, color, title);
	}
	//여기까지
	
	public MyView (Context context, Window window) {
		super (context);
		
		width = 300;
		height = 300;
		graph = new Graph(window);
		//invalidate();
	}
	
	public MyView (Context context, Window window, int _width, int _height) {
		super (context);
		
		width = _width;
		height = _height;
		graph = new Graph(window, 50, 50, width, height);
		//invalidate();
	}
	
	public MyView (Context context, Window window, int _width, int _height, float minX, float maxX, float minY, float maxY) {
		super (context);
		
		width = _width;
		height = _height;
		this.minX = minX;
		this.maxX = maxX;
		this.minY = minY;
		this.maxY = maxY;
		
		graph = new Graph(window, 45, 60, width, height, minX, maxX, minY, maxY);
		//invalidate();
	}
	
	public void setXUnit(int unit) {
		graph.unitType = unit;
	}
	
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		// height 진짜 크기 구하기
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = 0;
		switch (heightMode) {
		case MeasureSpec.UNSPECIFIED: // mode 가 셋팅되지 않은 크기가 넘어올때
			heightSize = heightMeasureSpec;
			break;
		case MeasureSpec.AT_MOST: // wrap_content (뷰 내부의 크기에 따라 크기가 달라짐)
			heightSize = 20;
			break;
		case MeasureSpec.EXACTLY: // fill_parent, match_parent (외부에서 이미 크기가
									// 지정되었음)
			heightSize = MeasureSpec.getSize(heightMeasureSpec);
			break;
		}

		// width 진짜 크기 구하기
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = 0;
		switch (widthMode) {
		case MeasureSpec.UNSPECIFIED: // mode 가 셋팅되지 않은 크기가 넘어올때
			widthSize = widthMeasureSpec;
			break;
		case MeasureSpec.AT_MOST: // wrap_content (뷰 내부의 크기에 따라 크기가 달라짐)
			widthSize = 100;
			break;
		case MeasureSpec.EXACTLY: // fill_parent, match_parent (외부에서 이미 크기가
									// 지정되었음)
			widthSize = MeasureSpec.getSize(widthMeasureSpec);
			break;
		}

		setMeasuredDimension(widthSize, heightSize);

		width = widthSize;
		height = heightSize;
		
		if(graph == null) {
			graph = new Graph(0, 0, width, height, 0, width, 0, height);
		} else {
			graph.setSize(width, height);
		}

	}
}
